package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.OldmanVolunteerInfoEntity;
import city.spring.modules.reception.service.OldmanVolunteerInfoService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：志愿者信息
 *
 * @author pxm
 * @date 2020-07-01 09:54:20
 */
@RestController
@RequestMapping("/oldman_volunteer_info")
public class OldmanVolunteerInfoController extends ApiController {
    private final OldmanVolunteerInfoService oldmanVolunteerInfoService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanVolunteerInfoEntity, ?>> entityOrderFields;

    public OldmanVolunteerInfoController(OldmanVolunteerInfoService oldmanVolunteerInfoService) {
        this.oldmanVolunteerInfoService = oldmanVolunteerInfoService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanVolunteerInfoEntity::getId);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getName);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getPhone);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getAddress);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getJob);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getSkills);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getStartTime);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getEndTime);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getPeriod);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getRemarks);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getGmtCreate);
        entityOrderFields.add(OldmanVolunteerInfoEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>志愿者信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanVolunteerInfoEntity entity) {
        LambdaQueryChainWrapper<OldmanVolunteerInfoEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>志愿者信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanVolunteerInfoEntity entity) {
        LambdaQueryChainWrapper<OldmanVolunteerInfoEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanVolunteerInfoEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取可以使用的志愿者列表（前端选择用户时使用的用户列表）
     *
     * @param pageable 分页信息
     */
    @GetMapping("selectVolunteerList")
    public Object selectVolunteerList(@PageableDefault Pageable pageable, OldmanVolunteerInfoEntity entity) {
        LambdaQueryChainWrapper<OldmanVolunteerInfoEntity> lambdaQuery = buildLambdaQuery(entity);
        lambdaQuery.select(OldmanVolunteerInfoEntity::getId, OldmanVolunteerInfoEntity::getName, OldmanVolunteerInfoEntity::getPhone, OldmanVolunteerInfoEntity::getJob, OldmanVolunteerInfoEntity::getAddress);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanVolunteerInfoEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>志愿者信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanVolunteerInfoService.getById(id));
    }

    /**
     * 添加一个 <strong>志愿者信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanVolunteerInfoEntity entity) {
        oldmanVolunteerInfoService.saveOldmanVolunteerInfo(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>志愿者信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanVolunteerInfoEntity entity) {
        entity.setId(id);
        oldmanVolunteerInfoService.updateOldmanVolunteerInfo(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>志愿者信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanVolunteerInfoService.deleteOldmanVolunteerInfo(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>志愿者信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanVolunteerInfoService.deleteOldmanVolunteerInfo(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanVolunteerInfoEntity> buildLambdaQuery(OldmanVolunteerInfoEntity entity) {
        LambdaQueryChainWrapper<OldmanVolunteerInfoEntity> lambdaQuery = oldmanVolunteerInfoService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanVolunteerInfoEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), OldmanVolunteerInfoEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), OldmanVolunteerInfoEntity::getPhone, entity.getPhone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAddress()), OldmanVolunteerInfoEntity::getAddress, entity.getAddress());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getJob()), OldmanVolunteerInfoEntity::getJob, entity.getJob());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSkills()), OldmanVolunteerInfoEntity::getSkills, entity.getSkills());
        lambdaQuery.eq(entity.getStartTime() != null, OldmanVolunteerInfoEntity::getStartTime, entity.getStartTime());
        lambdaQuery.eq(entity.getEndTime() != null, OldmanVolunteerInfoEntity::getEndTime, entity.getEndTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPeriod()), OldmanVolunteerInfoEntity::getPeriod, entity.getPeriod());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemarks()), OldmanVolunteerInfoEntity::getRemarks, entity.getRemarks());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanVolunteerInfoEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanVolunteerInfoEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanVolunteerInfoEntity> buildQuery(OldmanVolunteerInfoEntity entity) {
        QueryWrapper<OldmanVolunteerInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "phone", entity.getPhone());
        wrapper.like(StringUtils.isNotBlank(entity.getAddress()), "address", entity.getAddress());
        wrapper.like(StringUtils.isNotBlank(entity.getJob()), "job", entity.getJob());
        wrapper.like(StringUtils.isNotBlank(entity.getSkills()), "skills", entity.getSkills());
        wrapper.eq(entity.getStartTime() != null, "start_time", entity.getStartTime());
        wrapper.eq(entity.getEndTime() != null, "end_time", entity.getEndTime());
        wrapper.like(StringUtils.isNotBlank(entity.getPeriod()), "period", entity.getPeriod());
        wrapper.like(StringUtils.isNotBlank(entity.getRemarks()), "remarks", entity.getRemarks());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
